/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package util;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Encoder;

/**
 *
 * @author dgsi
 */
public class Encrypt {
 public static String[] encrip(String usr, String psw) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
           DESKeySpec keySpec = new DESKeySpec("odp123upn321".getBytes("UTF8"));
           SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
           SecretKey key = keyFactory.generateSecret(keySpec);

           // Encrypt digest
           Cipher cipher = Cipher.getInstance("DES");
           cipher.init(Cipher.ENCRYPT_MODE, key);
           //System.out.println("algorithm  " + key.getAlgorithm());
           //System.out.println("format  " + key.getFormat());
           //System.out.println("encoded  " + new String(key.getEncoded()));
           String user = new BASE64Encoder().encode(cipher.doFinal(usr.getBytes()));
           String pss = new BASE64Encoder().encode(cipher.doFinal(psw.getBytes()));
         //  System.out.println("enciptados "+ user+ " - "+ pss);

           String[] encrip= new String[]{user,pss};

           return encrip;
    }
}
